/**
 * Created by Administrator on 2017/1/21.
 */

var roomManager = require('../../../logic/world/roomManager');
var LogicSubModules = require("../../../logic/sub");

module.exports = function(app) {
    return new RoomRemote(app);
};

var RoomRemote = function(app) {
    this.app = app;
};

var remote = RoomRemote.prototype;

remote.destroyRoom = function(arg, callback) {
    logger.debug("world.RoomRemote.destroyRoom arg=%j", arg);
    callback();

    var roomId = arg.roomId;

    //---------------------------------------------------------------------
    // 更新代开房间
    var room = roomManager.getRoom(roomId);
    if (room.agency) {
        gPlayerManger.getPlayer(room.creator, function (thePlayer) {
            if (thePlayer) {

                thePlayer.send("onRoomsChange", {
                    id: roomId,
                    destroy: true
                });

            }
        });
    }
    //---------------------------------------------------------------------

    roomManager.delRoom(roomId);
}

remote.quitRoom = function(arg, callback) {
    logger.debug("world.RoomRemote.quitRoom arg=%j", arg);
    callback();

    var uid = arg.uid;

    //---------------------------------------------------------------------
    // 更新代开房间
    var self = this;
    var roomId = roomManager.getUserRoomId(uid);
    var room = roomManager.getRoom(roomId);
    if (room.agency) {
        gPlayerManger.getPlayer(room.creator, function (thePlayer) {
            if (thePlayer) {

                var msg = { rid: roomId };
                self.app.rpc.game.roomRemote.query({rid: roomId}, uid, msg, function (code, info) {
                    if (!code) {
                        thePlayer.send("onRoomsChange", info);
                    }
                });

            }
        });
    }
    //---------------------------------------------------------------------

    roomManager.userQuitRoom(uid);
};

/**
 * 远程调用更新金币场一局的结果
 * @param arg
 * @param callback
 */
remote.updateSceneSettlement = function (msg, callback) {
    var SubLogic = LogicSubModules[msg.gameType];
    if (!SubLogic) {
        callback(); return;
    }

    var subConfig = SubLogic.Config.SubType[msg.subType];
    if (!subConfig) {
        callback(); return;
    }

    var sceneBasicConfig = SubLogic.Config.Scenes ? SubLogic.Config.Scenes[msg.scene] : null;
    var activitySetting = (sceneBasicConfig) ? sceneBasicConfig.activitySetting : null;
    var syncUsers = {};

    async.each(ExObject.numberKeys(msg.players), function(uid, cb) {
        gPlayerManger.getPlayer(uid, function(player) {
            if (!player) {
                cb();
            }

            var stat = msg.players[uid];

            player.checkDayVars();
            player.alterResourceNotSync(ExObject.build('coin', stat.coin), "scene");
            player.checkDailyReward();

            if (activitySetting) {
                if (activitySetting.playReward) {
                    var playRound = player.addGameStat(msg.gameType, msg.scene, "playRound", 1);
                    if (playRound == activitySetting.playReward.round) {
                        player.alterResource(activitySetting.playReward.resource, "scenePlayRound");
                        player.send("onPlayerActivityReward", {
                            activity: "playReward",
                            resource:activitySetting.playReward.resource,
                            status: player.user.status
                        });
                    }
                }

                if (activitySetting.winReward && stat.score > 0) {
                    var winRound = player.addGameStat(msg.gameType, msg.scene, "winRound", 1);
                    if (winRound == activitySetting.winReward.round) {
                        player.alterResource(activitySetting.winReward.resource, "sceneWinRound");
                        player.send("onPlayerActivityReward", {
                            activity: "winReward",
                            resource:activitySetting.winReward.resource,
                            status: player.user.status
                        });
                    }
                }
            }

            syncUsers[uid] = {gameStat: player.getGameStat(msg.gameType, msg.scene),}
            cb();
        });
    }, function(err) {
        callback(syncUsers);
    });
};

/**
 * 同步更新房卡场局数
 * @param report
 * @param callback
 * @private
 * @user xiaolei
 */
RoomRemote.prototype.addCardRoomStat = function (report, callback) {

    // 获取对应的参数是否有效
    if (isNaN(report.curRound)) {
        return;
    }

    var players = report.players;
    for  (var index in players) {

        var uid = players[index].uid;

        if (isNaN(uid) || !players[index]) {
            continue;
        }

        // 需要对应玩家进行添加
        gPlayerManger.getPlayer(uid, function (player) {
            player.addCardRoomStat(players[index].score, report.curRound);
        }.bind(this));
    }

    callback();
};

/**
 * 远程调用更新比赛场结果
 * @param result
 * @param callback
 */
RoomRemote.prototype.updateMatchResult = function(result, callback) {
    callback();
    gMatchManager.updateResult(result);
}